package org.example.service.impl;

import org.apache.dubbo.config.annotation.Service;
import org.example.model.Code;
import org.example.service.CodeService;
import org.example.service.CodeService2;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.List;

@Service
public class CodeServiceImpl2 implements CodeService2 {

    @Autowired
    CodeService codeService;

    @Override
    public int validateEmail(String email, String code) {
        System.out.println("validateEmail email="+email+",code="+code);
        Code c = new Code();
        c.setEmail(email);
        List<Code> prev = codeService.findByEmailOrderByCreatetimeDesc(c);
        if(prev==null || prev.size()==0){
            return 1;
        }
        Code res = prev.get(0);
        if(res.getCode().equals(code)){
            if( res.getExpiretime().after(new Timestamp(System.currentTimeMillis()))) {
                return 0;
            } else {
                return 2; // code timeout
            }
        }
        return 1; // wrong code
    }
}
